package com.goldsprite.gameframeworks.input;

import com.badlogic.gdx.Input;
import com.goldsprite.gameframeworks.input.widgets.VirtualButton;
import com.goldsprite.gameframeworks.input.widgets.VirtualJoystick;
import com.goldsprite.gameframeworks.utils.math.Vector2;
import com.goldsprite.gameframeworks.PlatformImpl;

public class GameInputSystem extends InputSystem {
	private static Event<Object> onInitializationFinished = new Event<>();
	private static GameInputSystem instance;
	private static InputActions inputActions;

	public static void showAndroidKeyBoard(boolean isShow) {
		if (PlatformImpl.showSoftInputKeyBoard != null)
			PlatformImpl.showSoftInputKeyBoard.accept(isShow);
	}

	public static void addInputBinding(String name, InputBinding binding) {
		InputAction action = inputActions.getAction(name);
		if (action == null)
			return;

		action.addInputBinding(binding);
	}

	public static void onInitializationFinished(Runnable run) {
		onInitializationFinished.add(e -> {
			run.run();
		});
	}

	public static GameInputSystem getInstance() {
		if (instance == null) {
			instance = new GameInputSystem();
		}
		return instance;
	}

	protected GameInputSystem() {
		initInputActions();
		onInitializationFinished.invoke(null);
	}

	private VirtualJoystickBinding joystickBinding;
	private VirtualButtonBinding attackVirButtonBinding;
	private VirtualButtonBinding jumpVirButtonBinding;
	public static void setVirtualJoystick(VirtualJoystick joystick) {
		getInstance().joystickBinding.setJoystick(joystick);
	}
	public static void setAttackVirButton(VirtualButton virtualButton) {
		getInstance().attackVirButtonBinding.setVirtualButton(virtualButton);
	}
	public static void setJumpVirButton(VirtualButton virtualButton) {
		getInstance().jumpVirButtonBinding.setVirtualButton(virtualButton);
	}

	/**
	 * 输入行为配置类
	 */
	private static class InputActionConfig {
		String name;
		Object defaultValue;
		InputBinding[] bindings;

		public InputActionConfig(String name, Object defaultValue, InputBinding... bindings) {
			this.name = name;
			this.defaultValue = defaultValue;
			this.bindings = bindings;
		}
	}

	/**
	 * 定义输入行为配置表
	 */
	private InputActionConfig[] createInputConfigs() {
		return new InputActionConfig[]{
				// 移动控制
				new InputActionConfig("Move", new Vector2(),
						new Vector2KeyBinding("WASD", Input.Keys.W, Input.Keys.A, Input.Keys.S, Input.Keys.D),
						new Vector2KeyBinding("上下左右", Input.Keys.UP, Input.Keys.LEFT, Input.Keys.DOWN,Input.Keys.RIGHT),
						joystickBinding = new VirtualJoystickBinding()),
				// 攻击
				new InputActionConfig("Attack", false, 
						new KeyBinding("J", Input.Keys.J),
						attackVirButtonBinding = new VirtualButtonBinding()),
				// 跳跃
				new InputActionConfig("Jump", false, 
						new KeyBinding("K", Input.Keys.K),
						new KeyBinding("SPACE", Input.Keys.SPACE), jumpVirButtonBinding = new VirtualButtonBinding()),
				// 蹲下
				new InputActionConfig("Crouch", false, new KeyBinding(Input.Keys.CONTROL_LEFT)),
				// 加速
				new InputActionConfig("SpeedBoost", false, new KeyBinding(Input.Keys.SHIFT_LEFT)),
				// 受伤
				new InputActionConfig("Hurt", false, new KeyBinding(Input.Keys.Y)),
				// 死亡
				new InputActionConfig("Death", false, new KeyBinding(Input.Keys.Y)),
				// 重生
				new InputActionConfig("Respawn", false, new KeyBinding(Input.Keys.R)),
				// 切换角色
				new InputActionConfig("ChangeRole", false, new KeyBinding(Input.Keys.Q)),
				// 坐下
				new InputActionConfig("Sit", false, new KeyBinding(Input.Keys.Z))};
	}

	private void initInputActions() {
		// 创建按键行为表实例
		inputActions = new InputActions();
		setInputActions(inputActions);

		// 获取配置表
		InputActionConfig[] configs = createInputConfigs();

		// 批量创建和配置输入行为
		for (InputActionConfig config : configs) {
			InputAction action = new InputAction(config.name, config.defaultValue);

			// 添加所有输入绑定
			for (InputBinding binding : config.bindings) {
				action.addInputBinding(binding);
			}

			// 添加到行为表
			inputActions.addAction(config.name, action);
		}
	}

	public InputAction getInputAction(String name) {
		return inputActions.getAction(name);
	}
}

